package top.zhangjianyong.blog.portal.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zhangjianyong.blog.enums.RespCode;
import top.zhangjianyong.blog.entity.po.Blog;
import top.zhangjianyong.blog.params.comment.*;
import top.zhangjianyong.blog.response.ApiResponse;
import top.zhangjianyong.blog.service.BlogCommentService;
import top.zhangjianyong.blog.service.BlogService;

/**
 * 博客评论前台控制器
 *
 * @author zhangjianyong
 * @since 2024/12/19
 */
@RequestMapping("/portal/comment")
@RestController("portalComment")
public class BlogCommentController {

  private final BlogCommentService blogCommentService;
  private final BlogService blogService;

  public BlogCommentController(BlogCommentService blogCommentService, BlogService blogService) {
    this.blogCommentService = blogCommentService;
    this.blogService = blogService;
  }

  /** 保存评论 */
  @PostMapping("/save")
  public ApiResponse save(@RequestBody @Validated CommentSaveParam param) {
    Blog blog = blogService.getById(param.blogId());
    if (null == blog) return ApiResponse.fail(RespCode.BLOG_NOT_FUND);
    return blogCommentService.save(param);
  }

  /** 获取评论树 */
  @PostMapping("/tree")
  public ApiResponse getCommentTree(@RequestBody @Validated CommentTreeParam param) {
    return blogCommentService.getCommentTree(param);
  }

  /** 加载更多子评论 */
  @PostMapping("/load-more")
  public ApiResponse loadMoreChildren(@RequestBody @Validated LoadMoreChildrenParam param) {
    return blogCommentService.loadMoreChildren(param);
  }

  /** 点赞/取消点赞评论 */
  @PostMapping("/like")
  public ApiResponse likeComment(@RequestBody @Validated CommentLikeParam param) {
    return blogCommentService.likeComment(param);
  }

  /** 获取评论统计信息 */
  @GetMapping("/stats/{blogId}")
  public ApiResponse getCommentStats(@PathVariable Long blogId) {
    return blogCommentService.getCommentStats(blogId);
  }
}
